package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mapper.AcctRoleMapper;
import com.example.pojo.Acct;
import com.example.mapper.AcctMapper;
import com.example.pojo.AcctRole;
import com.example.pojo.Role;
import com.example.service.IAcctService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-03-27
 */
@Service
@Slf4j
public class AcctServiceImpl extends ServiceImpl<AcctMapper, Acct> implements IAcctService {

    @Autowired
    private AcctMapper acctMapper;

    @Autowired
    private AcctRoleMapper acctRoleMapper;

    @Override
    public PageInfo<Acct> getAccountsPaging(String accountName, String phone, Integer enabled, Date start, Date end, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Acct> list = acctMapper.selectAccountsPaging(accountName,phone,enabled,start,end,pageNum,pageSize);
        return new PageInfo<>(list);
    }

    /**
     * 批量删除账号及账号相关数据
     * @param accountIds
     * @return
     */
    @Override
    @Transactional
    public ResultVo deleteAccountBatch(Integer[] accountIds){
        int i = acctMapper.deleteBatchIds(Arrays.asList(accountIds));
        for (int j = 0; j < accountIds.length; j++) {
            acctRoleMapper.delete(new QueryWrapper<AcctRole>().eq("account_id",accountIds[j]));
        }
        if(i > 0){
            return ResultVo.success("删除成功。");
        }
        return ResultVo.error("删除失败!");
    }

    /**
     * 修改账号
     * @param account
     * @return
     */
    @Override
    public ResultVo updateAccount(Acct account) {
        int i = acctMapper.updateById(account);
        if(i > 0){
            List<Role> roleList = account.getRoleList();
            if(roleList != null && roleList.size() > 0){
                acctRoleMapper.delete(new QueryWrapper<AcctRole>().eq("account_id",account.getId()));//删除原来的权限
                roleList.forEach(role -> {//重新添加权限
                    acctRoleMapper.insert(new AcctRole(account.getId(),role.getId()));
                });
            }
            return ResultVo.success("修改成功。");
        }
        return ResultVo.error("修改失败!");
    }

    /**
     * 添加账户角色
     * @param account
     * @return
     */
    @Override
    @Transactional
    public ResultVo addAccount(Acct account) {
        int i = acctMapper.insert(account);
        if(i > 0){
            List<Role> roleList = account.getRoleList();
            if(roleList!=null && roleList.size()!=0){
                List<Integer> rIds = roleList.stream().map(Role::getId).collect(Collectors.toList());
                Integer accountId = account.getId();
                acctRoleMapper.addAcctRoles(accountId,rIds);
            }
            return ResultVo.success("添加成功。");
        }
        return ResultVo.error("添加失败!");
    }

    @Override
    public ResultVo updateAccountPwd(Acct account) {
        return ResultVo.error("暂无此操作！");
    }

    /**
     * 删除账号（及其账号权限）
     * @param accountId
     * @return
     */
    @Override
    @Transactional
    public ResultVo deleteAccount(Integer accountId) {
        int id = acctMapper.delete(new QueryWrapper<Acct>().eq("id", accountId));
        int d = acctRoleMapper.delete(new QueryWrapper<AcctRole>().eq("account_id",accountId));
        return ResultVo.success("删除账号成功");
    }
}
